package com.ruoyi.gp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.gp.domain.vo.ExcelPatientOptionVo;
import com.ruoyi.gp.domain.vo.PatientOptionVo;
import com.ruoyi.gp.enums.QuestionType;
import com.ruoyi.gp.mapper.PatientOptionMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import com.ruoyi.gp.domain.bo.PatientBo;
import com.ruoyi.gp.domain.vo.PatientVo;
import com.ruoyi.gp.domain.Patient;
import com.ruoyi.gp.mapper.PatientMapper;
import com.ruoyi.gp.service.IPatientService;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-04
 */
@RequiredArgsConstructor
@Service
public class PatientServiceImpl implements IPatientService {

    private final PatientMapper baseMapper;
    private final PatientOptionMapper patientOptionMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public PatientVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<PatientVo> queryPageList(PatientBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Patient> lqw = buildQueryWrapper(bo);
        Page<PatientVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<PatientVo> queryList(PatientBo bo) {
        LambdaQueryWrapper<Patient> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Patient> buildQueryWrapper(PatientBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Patient> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), Patient::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), Patient::getSex, bo.getSex());
        lqw.eq(bo.getAge() != null, Patient::getAge, bo.getAge());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), Patient::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getGoverningBody()), Patient::getGoverningBody, bo.getGoverningBody());
        lqw.eq(StringUtils.isNotBlank(bo.getManagementTeam()), Patient::getManagementTeam, bo.getManagementTeam());
        lqw.eq(StringUtils.isNotBlank(bo.getUserType()), Patient::getUserType, bo.getUserType());
        lqw.eq(bo.getFinish() != null, Patient::getFinish, bo.getFinish());
        lqw.eq(bo.getNotFinish() != null, Patient::getNotFinish, bo.getNotFinish());
        lqw.eq(bo.getMedicationRecordsId() != null, Patient::getMedicationRecordsId, bo.getMedicationRecordsId());
        lqw.eq(bo.getFamilyEnvironmentId() != null, Patient::getFamilyEnvironmentId, bo.getFamilyEnvironmentId());
        lqw.eq(bo.getRegister() != null, Patient::getRegister, bo.getRegister());
        lqw.eq(bo.getDoctorId() != null, Patient::getDoctorId, bo.getDoctorId());
        lqw.eq(bo.getMedicationRecordsCount() != null, Patient::getMedicationRecordsCount, bo.getMedicationRecordsCount());
        lqw.eq(bo.getFamilyEnvironmentCount() != null, Patient::getFamilyEnvironmentCount, bo.getFamilyEnvironmentCount());
        lqw.eq(bo.getFinishCount() != null, Patient::getFinishCount, bo.getFinishCount());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(PatientBo bo) {
        Patient add = BeanUtil.toBean(bo, Patient.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(PatientBo bo) {
        Patient update = BeanUtil.toBean(bo, Patient.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Patient entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public  TableDataInfo<Patient> queryByCount(Long doctorId, PageQuery pageQuery){
        QueryWrapper<Patient> queryWrapper = Wrappers.query();
        queryWrapper.eq(ObjectUtils.isNotNull(doctorId),"doctor_id",doctorId);
        Page<Patient> page = baseMapper.queryByCount(pageQuery.build(),queryWrapper);
        return  TableDataInfo.build(page);
    }

    @Override
    public List<ExcelPatientOptionVo> queryByOption(Long optionsId) {
        List<PatientOptionVo> patientOptionVos=patientOptionMapper.queryByOption(optionsId);
        List<ExcelPatientOptionVo> excelPatientOptionVos = new ArrayList<>();
        patientOptionVos.stream().forEach(patientOptionVo -> {
            patientOptionVo.setQuestionTypeName(
                QuestionType.getTypeName(patientOptionVo.getQuestionType())
            );
                ExcelPatientOptionVo excelPatientOptionVo = BeanUtil.toBean(patientOptionVo, ExcelPatientOptionVo.class);
                excelPatientOptionVos.add(excelPatientOptionVo);
        }
        );
        return excelPatientOptionVos;
    }
}
